//
//  productionViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/3.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "productionViewController.h"
#import "AFNetworking.h"
#import "homePageCellworkModel.h"
#import "homePageCellitemModel.h"
#import "homePageCellmerchantModel.h"
#import "homePageCellmediaModel.h"
#import "UIImageView+WebCache.h"
#import "oneSectionCellTableViewCell.h"
#import "twoSectionCellTableViewCell.h"
#import "MBProgressHUD.h"
#import "PlayViewController.h"
@interface productionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *homeArr;
@property(nonatomic,retain)NSMutableArray *homeitemArr;
@property(nonatomic,retain)NSMutableArray *homemerchantArr;
@property(nonatomic,retain)NSMutableArray *homemediaArr;
@property(nonatomic,retain)UITableView *tableview;
@property(nonatomic,retain)UIImageView *proImage;

@property(nonatomic,retain)MBProgressHUD *hud;

@property(nonatomic, assign)BOOL isArchiver;

@end

@implementation productionViewController
- (void)dealloc
{
   
    [_tableview release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBar.translucent =NO;
    self.title =@"作品Show";

    // 建立a反归档
    [self fanGuiDang];

    
    [self createData];
    
    [self createView];
    
    
    self.hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText =@"加载中......";
    
    // 右上角按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    if (self.isArchiver) {
        [button setImage:[UIImage imageNamed:@"tubiao1.png"] forState:UIControlStateNormal];
    } else {
        [button setImage:[UIImage imageNamed:@"tubiao2.png"] forState:UIControlStateNormal];
    }
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // 右上角
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

}

- (void)click:(UIButton *)button{
    
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Movie.plist"];
    
    self.homemerchantArr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    
    
    if (self.isArchiver) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
        NSInteger index = 0;
        for (NSInteger i = 0; i < self.homemerchantArr.count; i++) {
            homePageCellmerchantModel *vie = self.homemerchantArr[i];
            if ([vie.name isEqualToString:self.title]) {
                index = i;
                break;
            }
        }
        [self.homemerchantArr removeObjectAtIndex:index];
        
        // 对已经修改过的数组,再次进行归档操作
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Movie.plist"];
        NSLog(@"%@",documentPath);
        
        // 根据对应的路径进行归档操作
        [NSKeyedArchiver archiveRootObject:self.homemerchantArr toFile:documentPath];
        
        [button setImage:[UIImage imageNamed:@"tubiao2.png"] forState:UIControlStateNormal];
        self.isArchiver = NO;
        
    } else {
        
        
        // 进行归档操作
        //        [self.homemerchantArr addObject:self.home];
        
        
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Movie.plist"];
        
        
        if (self.homemerchantArr != nil) {
            [self.homemerchantArr addObject:self.home];
        } else {
            self.homemerchantArr = [NSMutableArray array];
            [self.homemerchantArr addObject:self.home];
        }
        // 根据对应的路径进行归档操作
        [NSKeyedArchiver archiveRootObject:self.homemerchantArr toFile:documentPath];
        
        self.isArchiver = YES;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
        [button setImage:[UIImage imageNamed:@"tubiao1.png"] forState:UIControlStateNormal];
        
        
        NSLog(@"%@", documentPath);
        
    }
    
}
#pragma mark 反归档
- (void)fanGuiDang{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    
    // 拼接
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Movie.plist"];
    
    self.homemerchantArr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    
    self.isArchiver = NO;
    if (self.homemerchantArr != nil) {
        for (homePageCellmerchantModel *mo in self.homemerchantArr) {
            if ([mo.name isEqualToString:self.name1]) {
                self.isArchiver = YES;
                break;
            }
        }
    }
}


- (void)createData
{
    self.homeArr = [NSMutableArray array];
    self.homemerchantArr = [NSMutableArray array];
    self.homemediaArr =[NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/v2/api/app/works/%@.json",self.bid] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSDictionary *dic = responseObject;
        NSDictionary *workDic = dic[@"work"];
        homePageCellworkModel *wo =[[homePageCellworkModel alloc]init];
        [wo setValuesForKeysWithDictionary:workDic];
        [self.homeArr addObject:wo];
        [wo release];
        
        NSDictionary *merchantDic = workDic[@"merchant"];
        self.home = [[homePageCellmerchantModel alloc]init];
        [self.home setValuesForKeysWithDictionary:merchantDic];
        [self.homemerchantArr addObject:self.home];
        [self.home release];
        
        NSArray *mediaArr = workDic[@"media_items"];
        for (NSDictionary *mediaDic in mediaArr) {
            homePageCellmediaModel *me = [[homePageCellmediaModel alloc]init];
            [me setValuesForKeysWithDictionary:mediaDic];
            [self.homemediaArr addObject:me];
            
            [me release];
        }
        
        
        
        [self.tableview reloadData];
        
//        NSLog(@"解析成功");
        
        [self.hud hide:YES afterDelay:2];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

}


- (void)createView
{
    //创建tableview
    self.tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableview.delegate =self;
    self.tableview.dataSource =self;

    [self.view addSubview:self.tableview];
    [_tableview release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 100;
    }
    else
    {
        return 200;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0) {
        return self.homemerchantArr.count;
    }
    else
    {
        return self.homemediaArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
                static NSString *reuse =@"reuse";
        oneSectionCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[oneSectionCellTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        homePageCellmerchantModel *mer =self.homemerchantArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:mer.logo_path]];
        cell.titleLabel.text =mer.name;
        cell.guanzhuLabel.text =[NSString stringWithFormat:@"关注 :%@",mer.fans_count];
        cell.zuopinLabel.text =[NSString stringWithFormat:@"作品 :%@",mer.works_count];
      
        return cell;
    }
    else
    {
        static NSString *reuse1 =@"reuse1";
        twoSectionCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (!cell) {
            cell = [[twoSectionCellTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        homePageCellmediaModel *me = self.homemediaArr[indexPath.row];
        if ([me.media_path hasSuffix:@"mov"]) {
            cell.bigImage.image =[UIImage imageNamed:@"视频"];
            
        }
        else
        {
        [cell.bigImage sd_setImageWithURL:[NSURL URLWithString:me.media_path]];
        }
        return cell;
    }
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==1) {
        
        homePageCellmediaModel *me =self.homemediaArr[indexPath.row];
        if ([me.media_path hasSuffix:@"mov"]) {
            
            PlayViewController *playVC = [[PlayViewController alloc]init];

            playVC.strurl =me.media_path;
            [self.navigationController pushViewController:playVC animated:YES];
            [playVC release];
        
        }
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
